package com.iptv.devices.service.impl;

import com.iptv.common.exception.ServiceException;
import com.iptv.common.utils.DateUtils;
import com.iptv.common.utils.StringUtils;
import com.iptv.common.utils.bean.BeanUtils;
import com.iptv.devices.domain.LiveDevices;
import com.iptv.devices.domain.dto.DeviceExternallyAddedDTO;
import com.iptv.devices.mapper.LiveDevicesMapper;
import com.iptv.devices.service.LiveDevicesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
* @author Administrator
* @description 针对表【live_devices】的数据库操作Service实现
* @createDate 2025-01-26 15:14:31
*/
@Service
public class LiveDevicesServiceImpl implements LiveDevicesService {

    @Resource
    private LiveDevicesMapper liveDevicesMapper;
    @Override
    public List<LiveDevices> selectDeviceListByView(LiveDevices liveDevices) {
        return liveDevicesMapper.selectDeviceListByView(liveDevices);
    }

    @Override
    public LiveDevices selectDeviceByDeviceId(Long deviceId) {
        return liveDevicesMapper.selectDeviceByDeviceId(deviceId);
    }

    @Override
    public int insertDevice(LiveDevices liveDevices) {
        return liveDevicesMapper.insertDevice(liveDevices);
    }

    @Override
    public int updateDevice(LiveDevices liveDevices) {
        return liveDevicesMapper.updateDevice(liveDevices);
    }

    @Override
    public int deleteDeviceByDeviceIds(Long[] deviceIds) {
        return liveDevicesMapper.deleteDeviceByDeviceIds(deviceIds);
    }

    @Override
    public int checkStatusByMac(String mac) {
        return liveDevicesMapper.checkStatusByMac(mac);
    }

    @Override
    public int checkRegistrationDevice(String mac) {
        return liveDevicesMapper.checkRegistrationDevice(mac);
    }

    @Override
    public int externallyAdded(DeviceExternallyAddedDTO dto) {
        if(StringUtils.isNull(dto.getMac())){
            throw new ServiceException("設備mac地址不能爲空");
        }
        // 克隆
        LiveDevices liveDevices = new LiveDevices();
        BeanUtils.copyProperties(dto, liveDevices);
        liveDevices.setCreateTime(DateUtils.getNowDate());
        liveDevices.setUpdateTime(DateUtils.getNowDate());
        // 设置默认过期时间为 2099-01-01
        Calendar calendar = Calendar.getInstance();
        calendar.set(2099, Calendar.JANUARY, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);  // 去掉毫秒部分

        liveDevices.setExpirationTime(calendar.getTime());

        return liveDevicesMapper.insertDevice(liveDevices);
    }

    @Override
    public int updateStatus(Long[] deviceIds) {
        return liveDevicesMapper.updateStatus(deviceIds);
    }

    @Override
    public LiveDevices getDeviceByMac(String mac) {
        return liveDevicesMapper.getDeviceByMac(mac);
    }


}




